package com.foreveross.chameleon;

import com.foreveross.chameleon.config.ChameleonConfig;
import com.foreveross.chameleon.manager.BroadcastManager;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

/**
 * @description 网络状态观察者
 * @author zhouzhineng
 *
 */
public class NetworkStateChangeReceiver extends BroadcastReceiver{

	@Override
	public void onReceive(Context ctx, Intent it) {
		// TODO Auto-generated method stub
		ConnectivityManager connectivityManager=(ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo  mobNetInfo=connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        NetworkInfo  wifiNetInfo=connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        boolean curNetState = ChameleonConfig.NETWORK_OK;
        if (!mobNetInfo.isConnected() && !wifiNetInfo.isConnected()) {
        	ChameleonConfig.NETWORK_OK = false;
        }else {
        	ChameleonConfig.NETWORK_OK = true;
        }
        if(curNetState != ChameleonConfig.NETWORK_OK) {
        	BroadcastManager.getInstance().sendNetworkState(ChameleonConfig.NETWORK_OK);
        }
	}

}
